var that,
  app = getApp(),
  api= new (require('../../utils/api.js')),
  headimg = api.getimgurl(), 
  util=new (require('../../utils/util')); 

Page({

  /**
   * 页面的初始数据 
   */
  data: {
    showModel:!1,
    showbModel:!1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    let {userid} = options;
    if(userid){
      let page = getCurrentPages();
      page= page[page.length-2];
      that.setData({data:page.data.seledata,userid,lastpage:page})
      that.getlist(userid)
      // api.super_getaccountlist(userid,res=>{
      //   if(res.data.issuccess==1){
      //     let {list} =res.data,newlist = [];
      //     list.forEach(x=>{
      //       x['datestr'] = util.formatTime(new Date(Number(x.updateDate)*1000))
      //       newlist.push(x)
      //     }) 
      //     that.setData({list:res.data.list,storage:res.data.storage||0})
      //   }else{
      //     util.nocancelmodal(res.data.msg)
      //   }

      // })


    }else{
      util.nocancelmodal('提示','参数错误',res=>{
        wx.navigateBack()
      })
    }

  },

  getlist(userid){
    api.super_getaccountlist(userid,res=>{
      if(res.data.issuccess==1){
        let {list} =res.data,newlist = [];
        list.forEach(x=>{
          x['datestr'] = util.formatTime(new Date(Number(x.updateDate)*1000))
          newlist.push(x)
        }) 
        that.setData({list:newlist,storage:res.data.storage||0})
      }else{
        util.nocancelmodal(res.data.msg)
      }

    })
  },


  setaccount(e){
    let {r} = e.currentTarget.dataset;
    that.setData({operate:r,showModel:!0})
  },

  cancel(e){
    that.setData({showModel:!1})
  },
  cancelb(e){
    that.setData({showbModel:!1})
  },
  setmoney(e){
    that.setData({inputmoney:e.detail.value})
  },
  setbeer(e){
    let {r} = e.currentTarget.dataset;
    that.setData({operate:r,showbModel:!0})
  },

  inputsetbeer(e){
    that.setData({inputbeer:e.detail.value})
  },

  confirm(e){
    let{operate,inputmoney,userid} = this.data,type=Number(operate)==0?'XF':'CZ';
    let data = {type,id:userid,point:Math.round(Number(inputmoney)*100)}; 
    console.log(data,'confirm--data');
    api.supersetaccount(data,res=>{
      if(res.data.issuccess==1){
        util.nocancelmodal('成功！','提示',res1=>{
          that.setData({showModel:!1,setmoneyval:'',['data.points']:res.data.points})
          that.getlist(that.data.userid)
          //api.supergetlist(res=>{
          //   if(res.data.issuccess==1){
          //     that.setData({list:res.data.data})
          //   }else{
          //     util.nocancelmodal(res.data.msg)
          //   }
          // })
        })
      }else{
        util.nocancelmodal(res.data.msg)
      }
    })
  },

  confirmb(e){
    let{operate,inputbeer,userid} = this.data,type=Number(operate)==0?'DBEER':'ABEER';
    let data = {type,id:userid,point:Number(inputbeer)};
    console.log(data,'confirm--data');
    api.supersetbeer(data,res=>{
      if(res.data.issuccess==1){
        util.nocancelmodal('成功！','提示',res1=>{
          that.setData({showbModel:!1,setbeerval:'',['data.beers']:res.data.beers})
that.getlist(userid)
          // api.supergetlist(res=>{
          //   if(res.data.issuccess==1){
          //     that.setData({list:res.data.data})
          //   }else{
          //     util.nocancelmodal(res.data.msg)
          //   }
          // })
        })
      }else{
        util.nocancelmodal(res.data.msg)
      }
    })
  },

  deluser(e){
    wx.showModal({
      title:'提示',
      content: '确定要删除吗',
      success:res=>{
        if(res.confirm){
          api.superdel(that.data.userid,res=>{
            if(res.data.issuccess==1){
              // api.supergetlist(res=>{
              //   if(res.data.issuccess==1){
              //     that.setData({list:res.data.data}) 
              //   }else{
              //     util.nocancelmodal(res.data.msg)
              //   }
              // })
              let {lastpage} = this.data;
              lastpage.setData({needfresh:!0},()=>{
                wx.navigateBack({
                  delta: 0,
                })
              })
            }else{
              util.nocancelmodal(res.data.msg)
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    that.data.lastpage.setData({needfresh:!0})
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    that.data.lastpage.setData({needfresh:!0})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})